<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="财年">
          <a-date-picker
            mode="year"
            placeholder="请选择财年"
            format='YYYY'
            valueFormat="YYYY"
            v-decorator="[ 'bgYear', validatorRules.bgYear]"
            style="width:100%"
            :open='yearShowOne'
            @openChange="openChangeOne"
            @panelChange="panelChangeOne"
           />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="季度">
          <a-select efault-value="form.bgQuarter"
            v-decorator="[ 'bgQuarter',validatorRules.bgQuarter]"
            placeholder="请输入季度"
            :getPopupContainer = "(target) => target.parentNode">
            <a-select-option value="1">第一季度</a-select-option>
            <a-select-option value="2">第二季度</a-select-option>
            <a-select-option value="3">第三季度</a-select-option>
            <a-select-option value="4">第四季度</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="月度">
          <a-month-picker placeholder="请输入月度"   format='MM' valueFormat="MM" v-decorator="[ 'bgMonth',validatorRules.bgMonth]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="费用时间">
          <a-range-picker format="YYYY-MM-DD"  v-decorator="[ 'bgDate',validatorRules.bgDate]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remarks', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JDate from '@/components/cn-form/JDate'
  export default {
    components: {  JDate},
    name: "TpmBudgetYearModal",
    data () {
      return {
        title:"操作",
        visible: false,
        yearShowOne:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          bgYear:{rules: [{ required: true, message: '请选择财年!' }]},
          bgQuarter:{rules: [{ required: true, message: '请选择季度!' }]},
          bgMonth:{rules: [{ required: true, message: '请选择月度!' }]},
          bgDate:{rules: [{ required: true, message: '请选择费用时间!' }]},
        },
        url: {
          add: window._CONFIG['domianTpmURL']+"/budget/tpmBudgetYear/saveOrUpdate",
          edit: window._CONFIG['domianTpmURL']+"/budget/tpmBudgetYear/saveOrUpdate",
        },
      }
    },
    created () {
      // console.log(this.form.bgStartDate)
    },
    methods: {
      moment,
      openChangeOne(status){
        //status是打开或关闭的状态
        if(status){
          this.yearShowOne =  true
        }
      },
      // 得到年份选择器的值
      panelChangeOne(value){
        this.form.setFieldsValue({
          bgYear: moment(value).format('YYYY')
        })
        this.yearShowOne = false
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        var bgDate =new Array();
        bgDate.push(record.bgStartDate);
        bgDate.push(record.bgEndDate);
        record.bgDate = bgDate;
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'remarks','bgYear','bgQuarter','bgMonth','bgDate'))
		  //时间格式化
        });

      },
      close () {
        this.yearShowOne = false
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
        this.yearShowOne = false
      },


    }
  }
</script>

<style lang="less" scoped>

</style>
